﻿using MediatR;
using Traceable.App.Application.Command.Aquaculture;
using Traceable.Domian;
using Traceable.ErrorCode;
using Traceable.Infastructure;

namespace Traceable.App.Application.Handler.Aquaculture
{
    public class DeleteImageHandler : IRequestHandler<DeleteImageCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<Image_Table> _imageRepository;

        public DeleteImageHandler(IBaseRepository<Image_Table> imageRepository)
        {
            _imageRepository = imageRepository;
        }
        /// <summary>
        /// 照片删除
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiResult<int>> Handle(DeleteImageCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();

            var image = _imageRepository.GetAll().Where(p =>p.Batch_Id==request.Batch_Id && p.Imgurl == request.Imgurl).FirstOrDefault();
            if (image != null)
            {
                 _imageRepository.Delete(image);
                result.Code = StatusEnum.OK;
                result.Data = 1;
                result.Message = "删除成功";
                return Task.FromResult(result);
            }
            result.Code = StatusEnum.Error;
            result.Data = 0;
            result.Message = "删除失败";
            return Task.FromResult(result);
        }
    }
}
